﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ActivityTracker.Library;
using Common.Library;


namespace ActivityTracker
{
    public partial class DeleteCategory : PhoneApplicationPage
    {
        #region Global

        Category objCategory = new Category();

        #endregion

        #region Constructor

        public DeleteCategory()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                ListBoxCategories.ItemsSource = objCategory.LoadCategories();
            }
            catch (Exception ex)
            {
                string mess = "Exception in PhoneApplicationPage_Loaded Method In ActivityTracker.Views.Categories.DeleteCategory file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void AppBarDelete_Click(object sender, EventArgs e)
        {
            try
            {
                bool runningActivityConfirmation = false;
                string DeleteConfirmation = "";
                List<string> actsToDelete = null;
                List<CategoryProp> catsToDelete = objCategory.DeleteCategoryConfirmation(ListBoxCategories, out DeleteConfirmation, out actsToDelete);
                foreach (string actId in actsToDelete)
                    if (App.runningActId == actId)
                        runningActivityConfirmation = true;
                if (runningActivityConfirmation)
                    MessageBox.Show("You cannot delete this category because one of the activity is running based on this category");
                else
                {
                    MessageBoxResult res = MessageBox.Show("Are you sure you want to delete this category?", DeleteConfirmation, MessageBoxButton.OKCancel);
                    if (res == MessageBoxResult.OK)
                    {
                        objCategory.DeleteCategories(catsToDelete, actsToDelete);
                        ListBoxCategories.ItemsSource = objCategory.LoadCategories();
                    }
                }
            }
            catch (Exception ex)
            {
                string mess = "Exception in AppBarDelete_Click Method In ActivityTracker.Views.Categories.DeleteCategory file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        #endregion
    }
}